package edu.hebeu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import edu.hebeu.R;
import edu.hebeu.network.APIConfig;

public class GoodDetailActivity extends BaseActivity {

    private Bundle good_data;
    private int buy_num;
    private ImageView iv_prod;
    private TextView tv_title;
    private TextView tv_money;
    private TextView tv_store;
    private TextView tv_msg;
    private TextView tv_address;
    private Button btn_sub;
    private Button btn_add;
    private EditText et_buy_num;
    private Button btn_add_car;

    @Override
    protected void initViews() {
        iv_prod = (ImageView) findViewById(R.id.iv_prod);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_money = (TextView) findViewById(R.id.tv_money);
        tv_store = (TextView) findViewById(R.id.tv_store);
        tv_msg = (TextView) findViewById(R.id.tv_msg);
        tv_address = (TextView) findViewById(R.id.tv_address);
        btn_sub = (Button) findViewById(R.id.btn_sub);
        btn_add = (Button) findViewById(R.id.btn_add);
        et_buy_num = (EditText) findViewById(R.id.et_buy_num);
        btn_add_car = (Button) findViewById(R.id.btn_add_car);
        getData();
        initListener();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_good_detail;
    }

    private void getData() {
        Intent intent = getIntent();
        good_data = intent.getBundleExtra("good_data");

        RequestOptions requestOptions = new RequestOptions()
            .placeholder(R.drawable.hold) // 正在请求图片时展示的图片
            .error(R.drawable.error) // 如果请求失败时展示的图片(如果没有设置，还是展示placeholder的展位符)
            .fallback(R.drawable.fallback) // 如果请求的url为null时展示的图片(如果没有设置，还是展示placeholder的占位符)
            .override(100, 100); // 上述几种状态显示的占位符图片的大小
        Glide.with(context)
            .load("http://150.158.212.186:3001" + good_data.getString("src"))
            .apply(requestOptions) // 设置请求该url在各个状态时控件的展位图片
            .into(iv_prod);

        tv_title.setText(good_data.getString("name") + "---" + good_data.getString("root_category_name"));
        tv_money.setText("￥" + good_data.getInt("price"));
        tv_store.setText("库存：" + good_data.getInt("store"));
        tv_msg.setText(good_data.getString("user_username") + "——" + good_data.getString("describe"));
        tv_address.setText("发货地址：" + good_data.getString("address"));

        if (good_data.getInt("store") <= 0) { // 如果库存小于0(即没有库存)
            btn_add.setEnabled(false);
            btn_sub.setEnabled(false);
            et_buy_num.setEnabled(false);
        }
    }

    /**
     * 这个方法用来初始化监听器
     */
    private void initListener() {
        et_buy_num.addTextChangedListener(new TextWatcher() {
            /**
             * 当文本框的值改变之前触发该方法
             * @param s
             * @param start
             * @param count
             * @param after
             */
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(TextUtils.isEmpty(s)){
                    // 没有变
                }else{
                    // 改变了
                }
            }

            /**
             * 当文本框的值改变之后触发该方法
             * @param s
             */
            @Override
            public void afterTextChanged(Editable s) {
                String numStr = et_buy_num.getText().toString();
                if (!numStr.equals("")) { // 如果文本框不为空
                    buy_num = Integer.parseInt(numStr); // 将文本框的值赋到buy_num变量
                } else { // 否则，即文本框的值为空
                    buy_num = 0; // 将buy_num变量赋值为0
                }
            }
        });
    }

    public void addNumClick(View view) {
        if (good_data.getInt("store") > 0 && buy_num < good_data.getInt("store")) {
            String num = ++buy_num + "";
            et_buy_num.setText(num);
        }
    }

    public void subNumClick(View view) {
        if (buy_num > 1) {
            String num = --buy_num + "";
            et_buy_num.setText(num);
        }
    }

    /**
     * 添加到购物车按钮被点击触发
     * @param view
     */
    public void addCarClick(View view) {
        if (buy_num > 0 && buy_num <= good_data.getInt("store")) { // 如果购买的数量大于0并且购买的数量小于等于库存
            addCar();
        } else {
            showToast("添加购物车失败！" + buy_num + "个", 0, false);
        }
    }

    /**
     * 添加到购物车的方法
     */
    private void addCar() {
        List<String[]> result = getDAO().query();
        for (String[] carItem : result) {
            if (carItem[0].equals(String.valueOf(good_data.getInt("id")))) { // 如果购物车中有该商品

                showToast("商品《" + good_data.getString("name") + "》+ " + buy_num, 0, false);
                buy_num = Integer.parseInt(carItem[3]) + buy_num; // 将本次添加到购物车的商品的数量 累加到购物城内的商品数量上
                // 修改购物车内商品的数量
                getDAO().update(String.valueOf(good_data.getInt("id")), good_data.getString("name"),
                        String.valueOf(good_data.getInt("store")), String.valueOf(buy_num),
                        String.valueOf(good_data.getInt("price")), good_data.getString("user_username"),
                        good_data.getString("src"), good_data.getString("describe"),
                        String.valueOf(good_data.getInt("index")),
                        String.valueOf(good_data.getInt("goods_category_id")));
                return;
            }
        }
        // 添加到购物车
        getDAO().insert(String.valueOf(good_data.getInt("id")), good_data.getString("name"),
                String.valueOf(good_data.getInt("store")), String.valueOf(buy_num),
                String.valueOf(good_data.getInt("price")), good_data.getString("user_username"),
                good_data.getString("src"), good_data.getString("describe"),
                String.valueOf(good_data.getInt("index")),
                String.valueOf(good_data.getInt("goods_category_id")));
        showToast("添加到购物车成功，" + buy_num + "个", 0, false);
    }
}
